package com.tanhua.back.controller;

import com.tanhua.back.Dto.PageResult;
import com.tanhua.back.service.FreezeService;
import com.tanhua.back.service.MessagesService;
import com.tanhua.back.service.UserService;
import com.tanhua.back.vo.MessagesVo;
import com.tanhua.back.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author maorf
 * @version 1.0
 */
@RestController
@RequestMapping("manage")
public class UserController {
    @Autowired
    private FreezeService freezeService;
    @Autowired
    private UserService userService;

    @Autowired
    private MessagesService messagesService;

    //冻结用户
    @PostMapping("users/freeze")
    public ResponseEntity freeze(@RequestBody Map map) {
        freezeService.freeze(map);
        return ResponseEntity.ok("操作成功");
    }

    //解冻用户
    @PostMapping("users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map map) {
        freezeService.unfreeze(map);
        return ResponseEntity.ok("操作成功");
    }


    @GetMapping("users")
    public ResponseEntity list(Integer pagesize, Integer page, Long id, String nickname, String city) {
        PageResult pageResult = userService.list(page, pagesize, id, nickname, city);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("users/{userID}")
    public ResponseEntity findById(@PathVariable Long userID) {
        UserInfoVo userInfoVo = userService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }


    //消息通过
    @PostMapping("/messages/pass")
    private ResponseEntity pass(@RequestBody String[] ids) {

        messagesService.pass(ids);
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("message", "消息通过:ea amet cillum");
        return ResponseEntity.ok(stringStringMap);
    }

    //消息置顶
    @PostMapping("messages/{id}/top")
    public ResponseEntity MessageTop(@PathVariable("id") Long id) {
        messagesService.MessageTop(id);
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("message", "消息置顶成功");
        return ResponseEntity.ok(stringStringMap);
    }

    //消息取消置顶
    @PostMapping("messages/{id}/untop")
    public ResponseEntity MessageUntop(@PathVariable("id") Long id) {
        messagesService.MessageUntop(id);
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("message", "消息取消置顶成功");
        return ResponseEntity.ok(stringStringMap);
    }

    //消息拒绝
    @PostMapping("/messages/reject")
    public ResponseEntity messagesReject(@RequestBody String[] userId) {
        messagesService.messagesReject(userId);
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("message", "操拒绝成功");
        return ResponseEntity.ok(stringStringMap);

    }

    /**
     * 消息详情
     *
     * @param messageId
     * @return
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity getMessageById(@PathVariable("id") String messageId) {
        MessagesVo vo = messagesService.getMessageById(messageId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 消息翻页
     *
     * @param page
     * @param pagesize
     * @param sortProp
     * @param sortOrder
     * @param id
     * @param sd
     * @param uid
     * @param ed
     * @param state
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity PageList(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String sortProp, String sortOrder, String id,
                                   String uid,
                                   Long sd, Long ed, String state) {
        PageResult result = messagesService.PageList(uid, page, pagesize, sortProp, sortOrder, id, sd, ed, state);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/messages/revocation")
    public ResponseEntity messagesRevocation(@RequestBody String[] movementIds) {
        String message = messagesService.messagesRevocation(movementIds);
        return ResponseEntity.ok(message);
    }

    /**
     * 登录记录
     *
     * @param uid
     * @param page
     * @param pagesize
     * @param sortProp
     * @param sortOrder
     * @return
     */
    @GetMapping("logs")
    public ResponseEntity messagesRevocation(Long uid, @RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize,
                                             String sortProp, String sortOrder) {
        PageResult pr = messagesService.findLogsByPage(uid, page, pagesize, sortProp, sortOrder);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 String sortProp, String sortOrder, Long uid) {
        PageResult result = userService.videos(page, pagesize, sortProp, sortOrder, uid);
        return ResponseEntity.ok(result);
    }

    //评论列表翻页
    @GetMapping("message/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   String sortProp, String sortOrder, Long messageID) {
        PageResult pr = userService.comments(page, pagesize, sortProp, sortOrder, messageID);
        return ResponseEntity.ok(pr);
    }
}
